<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
               label-width="110px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="考核部门" prop="deptId">
              <el-select v-model="dataForm.deptId" placeholder="请选择" filterable clearable :disabled="!dataForm.id ? false : true"
                         @change="handleChange($event)"
              >
                <el-option
                  v-for="(item ,index) in  deptList"
                  :key="index"
                  :label="item.deptName"
                  :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被考核部门" prop="bdeptId">
              <el-select v-model="dataForm.bdeptId" placeholder="请选择" filterable clearable :disabled="!dataForm.id ? false : true"
                         @change="handleDirChange($event)">
                <el-option
                  v-for="(it ,index) in deptBList"
                  :key="index"
                  :label="it.name"
                  :value="it.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item
              label="考核类别"  prop="assetTypeId">

              <el-select v-model="dataForm.assetTypeId" placeholder="请选择" filterable clearable :disabled="!dataForm.id ? false : true"
                         @change="assetTypeChange($event)">
                <el-option
                  v-for="(ite ,index) in  assetTypeList"
                  :key="index"
                  :label="ite.name"
                  :value="ite.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="指标名称" prop="indicatorTaskName">
              <el-select v-model="dataForm.indicatorTaskName" filterable clearable
                         :disabled="!dataForm.id ? false : true"
                         placeholder="请选择" allow-create
                         :loading="approveLoading" @change="getAssessmentContent($event)" style="width: 100%">
                <el-option
                  v-for="item in assessmentContentList"
                  :key="item"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="扣分" prop="assessRuleScore">
              <el-input-number v-model="dataForm.assessRuleScore" :precision="2" :step="0.1" :min="-9.5"
                               :max="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成情况" prop="complete">
              <el-input type="textarea" v-model="dataForm.complete" placeholder="完成情况"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="考核标准" prop="assetStandard">
              <li v-for="(item) in dataForm.assetStandard">
                <span>{{item}}</span>
              </li>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </span>
  </el-dialog>


  <!-- 弹窗, 新增 / 修改 -->

</template>

<script>
  import axios from 'axios'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        approveLoading: false,
        visible: false,
        rowDeptId: '',// 传过来的部门id
        // 考核规则
        dataForm: {
          id: 0,
          bdeptId: '',
          bdeptName: '',
          deptId: '',
          deptName: '',
          indicatorTaskName: '',
          complete: '',
          assessRuleScore: 0,
          assetTypeId: '',
          assetType: '',
          assetStandard: []

        },
        assetTypeList: [],
        standard: {
          id: null,
          name: null
        },
        deptBList: [],
        deptList: [],
        //规则数组
        examinationRulesNameList: [],
        assessmentContentList: [],
        standardList: [],

        rulesList: [],
        dataRule: {
          bdeptId: [
            { required: true, message: '被考核部门名称不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '考核部门名称不能为空', trigger: 'blur' }
          ],
          indicatorTaskName: [
            { required: true, message: '指标名称不能为空', trigger: 'blur' }
          ],
          complete: [
            { required: true, message: '完成情况不能为空', trigger: 'blur' }
          ],
          assessRuleScore: [
            { required: true, message: '扣分分不能为空', trigger: 'blur' }
          ],
          assetTypeId: [
            { required: true, message: '考核类别不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick (() => {
          this.$refs['dataForm'].resetFields ()
          this.dataForm.assetTypeId = ''
          this.dataForm.indicatorTaskName = ''

          if (this.dataForm.id) {
            axios ({
              url: `/hr-xc-human-resources-perform-manage/generator/depperformance/queryRuleById/${this.dataForm.id}`,
              method: 'get'
            }).then (({ data }) => {
              if (data && data.code === '00000') {
                var data = AesDecrypt (data.result)
                this.dataForm.bdeptId = data.bdeptId
                this.dataForm.bdeptName = data.bdeptName
                this.dataForm.deptId = data.deptId
                this.dataForm.deptName = data.deptName
                this.dataForm.indicatorTaskName = data.indicatorTaskName
                this.dataForm.complete = data.complete
                this.dataForm.assessRuleScore = data.assessRuleScore
                this.dataForm.assetStandard = data.assetStandard
                this.dataForm.assetTypeId = data.assetTypeId
                this.dataForm.assetType = data.assetType
              }
            }).catch (error => {
              this.$message.error ('服务器访问异常，请从新登录')
            })
          }
        })
        // 查询考核规则

        this.getExaminationRulesNameList ()
        // 被考核部门
        this.getDeptBList ()
        // 考核部门
        this.getDeptList ()
        // 获得考核类别
        this.getAssetTypeList ()

      },
      getExaminationRulesNameList() {
        this.examinationRulesNameList = []
        axios ({
          url: '/hr-xc-human-resources-perform-manage/generator/depperformance/queryAllRuleNames',
          method: 'get',
          params: { 'ruleName': null, 'assetContent': null }
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.examinationRulesNameList = data.result
          } else {
            this.$message.warning (data.message)
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },

      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate ((valid) => {
          if (valid) {
            this.$confirm (`确定保存或编辑操作?`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then (() => {

              var uu = {
                'id': this.dataForm.id || undefined,
                'bdeptId': this.dataForm.bdeptId,
                'bdeptName': this.dataForm.bdeptName,
                'deptId': this.dataForm.deptId,
                'deptName': this.dataForm.deptName,
                'assetTypeId': this.dataForm.assetTypeId,
                'assetType': this.dataForm.assetType,
                'indicatorTaskName': this.dataForm.indicatorTaskName,
                'assetStandard': this.dataForm.assetStandard,
                'assessRuleScore': this.dataForm.assessRuleScore,
                'complete': this.dataForm.complete
              }
              var jsonString = JSON.stringify (uu)
              var yy = Encrypt (jsonString)
              var encodedString = encodeURIComponent (yy)
              axios ({
                url: `/hr-xc-human-resources-perform-manage/generator/depperformance/${!this.dataForm.id ? 'create' : 'update'}`,
                method: 'post',
                data: encodedString
              }).then (({ data }) => {
                if (data && data.code === '00000') {
                  this.$message ({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit ('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error ('操作失败！')
                }
              }).catch (error => {
                this.$message.error ('服务器访问异常，请从新登录')
              })

            })
          }
        })
      },
      // 判断list有没有重复
      isRepeat(arr) {
        var hash = {}
        for (var i in arr) {
          if (hash[arr[i]]) {
            return true
          }
          hash[arr[i]] = true
        }
        return false
      },

      assetTypeChange(val) {
        let dir = this.assetTypeList.find ((item) => item.id === val)
        this.dataForm.assetType = dir.name
        this.dataForm.indicatorTaskName = null
        this.dataForm.assessRuleScore = null
        this.assessmentContentList = []
        this.dataForm.assetStandard = []
        axios ({
          url: '/hr-xc-human-resources-perform-manage/generator/depperformance/queryAllRuleNames',
          method: 'get',
          params: { 'ruleName': this.dataForm.assetType, 'assetContent': null }
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.assessmentContentList = data
          } else {
            this.$message.warning (data.message)

          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },

      getAssessmentContent(val) {
        this.dataForm.assessRuleScore = null
        this.standardList = []
        this.dataForm.assetStandard = []
        axios ({
          url: '/hr-xc-human-resources-perform-manage/generator/depperformance/queryAllRuleNames',
          method: 'get',
          params: { 'ruleName': this.dataForm.assetType, 'assetContent': val }
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.standardList = data
            this.standardList.forEach (v => {
              this.dataForm.assetStandard.push (v.standard)
            })
          }else {
            this.$message.warning (data.message)
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },

      getAssetTypeList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/depperformance/queryAllDeptCategory'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.assetTypeList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 查询被考核部门
      getDeptBList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/torgdept/queryAllDeptIdAndName'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.deptBList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      handleDirChange(val) {
        let dir = this.deptBList.find ((item) => item.id === val)
        this.dataForm.bdeptName = dir.name
      },
      handleChange(val) {
        let dir = this.deptList.find ((item) => item.deptId === val)
        this.dataForm.deptName = dir.deptName
      },
      getDeptList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/queryAllAssetDept'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            this.deptList = data.result
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      getassessRule(val) {
        let dir = this.standardList.find ((item) => item.standard === val)
        this.dafenForm.dynamicItem.forEach (v => {
          if (val === v.assessmentStandard) {
            v.assessRuleId = dir.id
          }
        })
      }
    }
  }
</script>

<style scoped>
  .el-col {
    margin-bottom: 14px;
  }

  .el-select {
    width: 100%;
  }

  .el-input-number {
    width: 79%;
  }
</style>
